package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.ResultCode;
import com.example.dao.ClassInfoDao;
import com.example.dao.TeacherInfoDao;
import com.example.dao.ZhuanyeInfoDao;
import com.example.entity.*;
import com.example.entity.ClassInfo;
import com.example.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Service
public class ClassInfoService {
    @Resource
    private ClassInfoDao classInfoDao;
    @Autowired
    private ZhuanyeInfoDao zhuanyeInfoDao;
    @Autowired
    private TeacherInfoDao teacherInfoDao;

    public void add(ClassInfo classInfo) {
        ClassInfo info=classInfoDao.findByName(classInfo.getName());

        if(ObjectUtil.isNotEmpty(info)){
            throw  new CustomException("-1","公司名重复");
        }

        classInfoDao.insertSelective(classInfo);
    }


    public List<ClassInfo> findAll() {//不用了
        List<ClassInfo> list = classInfoDao.selectAllClass();
        for(ClassInfo classInfo : list){
            if(ObjectUtil.isNotEmpty(classInfo.getZhuanyeid())) {
                ZhuanyeInfo zhuanyeInfo = zhuanyeInfoDao.selectById(classInfo.getZhuanyeid());
                classInfo.setZhuanyeName(zhuanyeInfo.getName());
            }
        }
        return list;
    }

    public void update(ClassInfo classInfo) {

        classInfoDao.updateById(classInfo.getId(),
                classInfo.getName(),classInfo.getDecription(),classInfo.getZhuanyeid()
                ,classInfo.getScore(),classInfo.getTeacherid(),classInfo.getKaiban()
                ,classInfo.getTime(),classInfo.getLocation(),classInfo.getYixuan());//全部

    }

    public void deleteById(Long id) {
        classInfoDao.deleteById(id);
    }

    public PageInfo<ClassInfo> findPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ClassInfo> list = classInfoDao.selectAllClass();
        for(ClassInfo classInfo : list){
            if(ObjectUtil.isNotEmpty(classInfo.getZhuanyeid())) {
                ZhuanyeInfo zhuanyeInfo = zhuanyeInfoDao.selectById(classInfo.getZhuanyeid());
                classInfo.setZhuanyeName(zhuanyeInfo.getName());
            }
            if(ObjectUtil.isNotEmpty(classInfo.getTeacherid())){
                TeacherInfo teacherInfo = teacherInfoDao.selectById(classInfo.getTeacherid());
                classInfo.setTeacherName(teacherInfo.getName());
            }
        }
        return PageInfo.of(list);
    }

    public PageInfo<ClassInfo> findPageName(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        List<ClassInfo> list =classInfoDao.findByNamePage(name);
        for(ClassInfo classInfo : list){
            if(ObjectUtil.isNotEmpty(classInfo.getZhuanyeid())) {
                ZhuanyeInfo zhuanyeInfo = zhuanyeInfoDao.selectById(classInfo.getZhuanyeid());
                classInfo.setZhuanyeName(zhuanyeInfo.getName());
            }
            if(ObjectUtil.isNotEmpty(classInfo.getTeacherid())){
                TeacherInfo teacherInfo = teacherInfoDao.selectById(classInfo.getTeacherid());
                classInfo.setTeacherName(teacherInfo.getName());
            }
        }
        return PageInfo.of(list);
    }

}
